package com.lalosoft.gastostarjetaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.repackaged.org.json.JSONObject;
import com.lalosoft.gastostarjetaweb.Constants;
import com.lalosoft.gastostarjetaweb.dao.UserDao;
import com.lalosoft.gastostarjetaweb.model.User;

@SuppressWarnings("serial")
public class GuardarUserServlet extends HttpServlet{
	
	private static final Logger log = Logger.getLogger("GuardarUser");

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String email = req.getParameter("email");

		User userFounded = UserDao.findUserByEmail(email);
		
		if(userFounded == null){
			User user = new User();
			user.setEmail(email);
			user.setUsername(username);
			user.setPassword(password);
			String token = UserDao.saveUser(user);
			try {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("token", token);
				jsonObject.put("result", "ok");
				sendJson(jsonObject, resp);
//				//enviar token
//				resp.setContentType("application/json");
//				// Get the printwriter object from response to write the required json object to the output stream      
//				PrintWriter out = resp.getWriter();
//				// Assuming your json object is **jsonObject**, perform the following, it will return your json object  
//				out.print(jsonObject);
//				out.flush();
			} catch (Exception e) {
				log.log(Level.WARNING, "Error en guardar usuario: " + e.getMessage(), e);
			}
		} else {
			try {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("result", "error");
				jsonObject.put("cause", "user exist");
				sendJson(jsonObject, resp);
//				resp.setContentType("application/json");
//				// Get the printwriter object from response to write the required json object to the output stream      
//				PrintWriter out = resp.getWriter();
//				// Assuming your json object is **jsonObject**, perform the following, it will return your json object  
//				out.print(jsonObject);
//				out.flush();
			} catch (Exception e) {
				log.log(Level.WARNING, "Error en guardar usuario: " + e.getMessage(), e);
			}
		}
	}

	private void sendJson(JSONObject json, HttpServletResponse resp) throws IOException{
		log.log(Level.WARNING, "JsonEnviado: " + json.toString());
		resp.setContentType(Constants.CONTENT_TYPE);
		// Get the printwriter object from response to write the required json object to the output stream      
		PrintWriter out = resp.getWriter();
		// Assuming your json object is **jsonObject**, perform the following, it will return your json object  
		out.print(json);
		out.flush();
	}
}
